# 1.uzdevums (A)
x <- c(100)
for (i in 1:20) { 
   last <- x[length(x)]
   x <- c(x,  round(last *1.05, digits=2))
}
x
length(x)

# 1.uzdevums (B)
x <- c(100)
for (i in 1:240) { 
   last <- x[length(x)]
   x <- c(x,  round(last * (1 + 0.05/12), digits=2))
}
x
length(x)

# 2.uzdevums
numExperiments <- 1000
alist <- blist <- clist <- rep(0,99)
for (N in 1:99) {
   for (experiment in 1:numExperiments) {
       # nejausi pieskir 100 dzivoklu novertejumus no 0 lidz 1
       flats <- runif(100, min=0, max=1)
       # atrod labako starp pirmajiem N (kurus palaida garam)
       firstNmax <- max(flats[1:N])
       # atlikusie dzivokli, kuri parsniedz firstNmax
       remainingFlats <- flats[(N+1):100]
       remainingFlats <- remainingFlats[remainingFlats > firstNmax]
       
       # (C) paliek bez, ja nekas labs vairs nav atlicis
       if (length(remainingFlats) == 0) { clist[N] <- clist[N] +  1 }
       # (B) panem ne-vislabako, ja velak butu vel labaks 
       else if (remainingFlats[1] < max(remainingFlats)) { blist[N] <- blist[N] + 1 }
       # (A) panem vislabako, ja neizpildas ne (C) ne (B)
       else { alist[N] <- alist[N] + 1 }
  }
}
plot(alist/numExperiments, type="l", col="red", lwd=2, xlim=c(0,100), ylim=c(0,1))
lines(blist/numExperiments, type="l", col="gray", lwd=2)
lines(clist/numExperiments, type="l", col="blue", lwd=2)
grid()

maxA <- max(alist)
bestN <- which(maxA == alist)[1]
bestSuccessRate <- maxA/numExperiments
print(paste("Optimalais N ir ", bestN, sep=""))
print(paste("Biezums, ar kuru atrod vislabako ir ", bestSuccessRate, sep=""))



# 3. uzdevums
withoutVAT <- function(x) {round(x/1.21, digits=2)  }
getVAT <- function(x) {round(x*0.21, digits=2)}
# rukisa aprekins
prices <- 1:100
pricesNoVAT <- withoutVAT(prices)
VAT <- prices - pricesNoVAT
# RID aprekins
correctVAT <- getVAT(pricesNoVAT)
# realu skaitlu salidzinasana :)
VAT == correctVAT
print(paste("vat[1]=",VAT[1], sep=""))
print(paste("correctVAT[1]=",correctVAT[1], sep=""))
print(paste("vat[1]-correctVAT[1]=",VAT[1]-correctVAT[1], sep=""))

# uzlabota realu skaitlu salidzinasana
# atrod atskirigos (tipiski atskiriba ir par 1 sant.)
badLines <- abs(VAT - correctVAT) > 1E-7
# saskaita, cik ir vertibu TRUE
sum(badLines)
# saskaita rukisa izdevumus
sum(badLines) + sum(VAT)

round(3.30*0.21, digits=2) + 3.30
round(3.31*0.21, digits=2) + 3.31


# 4. uzdevums
install.packages("Rlab")
library(Rlab)
numExperiments <- 10000
alist <- blist <- clist <- rep(0,numExperiments)
for (experiment in 1:numExperiments) {
  x <- rbern(10000, 1/365)
  xx <- cumsum(x)
  alist[experiment] <- which(xx == 1)[1]
  blist[experiment] <- which(xx == 2)[1]
  clist[experiment] <- sum(x)
}
sum(alist)/numExperiments
sum(blist)/numExperiments
sum(clist)/numExperiments


# 5.uzdevums
install.packages("Rlab")
library(Rlab)
x <- rbern(1000000,0.5)
xx <- 2*x - 1
pxx <- cumsum(xx)

payoffs <- sign(pxx)
table(payoffs)

plot(1:1000000, pxx, type="l")
abline(h=0)


# 6. uzdevums
pr <- c(1)
for (i  in 1:365) {
  pr <- c(pr,pr[i]*(365-i)/365)
}
which(pr < 1/2)[1]
pr


# 7. uzdevums
# Simule 1000 cipsu pakas
x <- floor(12*runif(1000)) + 1
y <- rep(0,12)
for (i in 1:1000) {
  y[i] <- length(table(x[1:i]))
}
which(y==12)[1]

